package com.grass.appointment.activity;

import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.alibaba.android.arouter.launcher.ARouter;
import com.androidx.p013lv.base.Constance;
import com.androidx.p013lv.base.bean.OfficialBean;
import com.androidx.p013lv.base.bean.OfficialListBean;
import com.androidx.p013lv.base.bean.event.ChangeCityEvent;
import com.androidx.p013lv.base.glide.GlideUtils;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.listener.ItemClickListener;
import com.androidx.p013lv.base.p015ui.BaseActivity;
import com.androidx.p013lv.base.utils.LogUtils;
import com.androidx.p013lv.base.utils.NetUtil;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.grass.appointment.C2694R;
import com.grass.appointment.adapter.OfficialAdapter;
import com.grass.appointment.databinding.ActivityOfficialLayoutBinding;
import com.grass.appointment.dialog.AddressDialog;
import com.grass.appointment.model.OfficialModel;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import java.util.List;
import java.util.Locale;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import p000a.p002b.RxPermissions;

public class OfficialActivity extends BaseActivity<ActivityOfficialLayoutBinding> implements ItemClickListener, OnRefreshLoadMoreListener {
    private OfficialAdapter adapter;
    private AddressDialog addressDialog;
    public String backTitle;
    private String city = "全国";
    private boolean isResume = false;
    boolean isTimeOut;
    private int lastId;
    LocationListener locationListener = new LocationListener() {
        /* class com.grass.appointment.activity.OfficialActivity.C27215 */

        public void onLocationChanged(Location location) {
            LogUtils.m63e("tagsodf", "onLocationChanged");
            OfficialActivity.this.updateLocation(location);
        }

        public void onStatusChanged(String str, int i, Bundle bundle) {
            LogUtils.m63e("tagsodf", "onStatusChanged");
        }

        public void onProviderEnabled(String str) {
            LogUtils.m63e("tagsodf", "onProviderEnabled");
            try {
                OfficialActivity.this.updateLocation(OfficialActivity.this.mLocationManager.getLastKnownLocation(str));
            } catch (SecurityException e) {
                e.printStackTrace();
                LogUtils.m63e("tagsodf", "onProviderEnabled123");
            }
        }

        public void onProviderDisabled(String str) {
            LogUtils.m63e("tagsodf", "onProviderDisabled");
        }
    };
    LocationManager mLocationManager;
    private OfficialModel model;

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(((ActivityOfficialLayoutBinding) this.binding).toolbar).init();
    }

    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public int setLayout() {
        return C2694R.C2699layout.activity_official_layout;
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initView() {
        super.initView();
        ((ActivityOfficialLayoutBinding) this.binding).setClick(new OnClick());
        ((ActivityOfficialLayoutBinding) this.binding).setBackTitle(this.backTitle);
        EventBus.getDefault().register(this);
        OfficialModel officialModel = (OfficialModel) new ViewModelProvider(this).get(OfficialModel.class);
        this.model = officialModel;
        officialModel.data().observe(this, new Observer<BaseRes<OfficialListBean>>() {
            /* class com.grass.appointment.activity.OfficialActivity.C27171 */

            public void onChanged(BaseRes<OfficialListBean> baseRes) {
                ((ActivityOfficialLayoutBinding) OfficialActivity.this.binding).statusLayout.hideLoading();
                ((ActivityOfficialLayoutBinding) OfficialActivity.this.binding).refresh.finishRefresh();
                ((ActivityOfficialLayoutBinding) OfficialActivity.this.binding).refresh.finishLoadMore();
                if (baseRes.getCode() == 200) {
                    if (baseRes.getData() == null || baseRes.getData().getList() == null || baseRes.getData().getList().size() <= 0) {
                        if (OfficialActivity.this.lastId == 0) {
                            ((ActivityOfficialLayoutBinding) OfficialActivity.this.binding).statusLayout.showEmpty();
                        } else {
                            ((ActivityOfficialLayoutBinding) OfficialActivity.this.binding).refresh.finishLoadMoreWithNoMoreData();
                        }
                    } else if (OfficialActivity.this.lastId == 0) {
                        if (!baseRes.getData().getCityName().equals(OfficialActivity.this.city) && OfficialActivity.this.isResume) {
                            ToastUtils.getInstance().showSigh("當前城市無樓鳳信息，看一下全國的小姐姐吧");
                        }
                        OfficialActivity.this.city = baseRes.getData().getCityName();
                        ((ActivityOfficialLayoutBinding) OfficialActivity.this.binding).setCityAddress("當前位置：" + OfficialActivity.this.city);
                        OfficialActivity.this.adapter.setDomain(baseRes.getData().getDomain());
                        OfficialActivity.this.adapter.setData(baseRes.getData().getList());
                        ((ActivityOfficialLayoutBinding) OfficialActivity.this.binding).refresh.resetNoMoreData();
                    } else {
                        OfficialActivity.this.adapter.setDatasInEnd(baseRes.getData().getList());
                    }
                } else if (OfficialActivity.this.lastId == 0) {
                    ((ActivityOfficialLayoutBinding) OfficialActivity.this.binding).statusLayout.showError();
                } else {
                    ToastUtils.getInstance().showWeak(baseRes.getMsg());
                }
            }
        });
        ((ActivityOfficialLayoutBinding) this.binding).refresh.setOnRefreshListener(this);
        ((ActivityOfficialLayoutBinding) this.binding).refresh.setOnLoadMoreListener(this);
        this.adapter = new OfficialAdapter();
        ((ActivityOfficialLayoutBinding) this.binding).recycler.setLayoutManager(new LinearLayoutManager(this));
        ((ActivityOfficialLayoutBinding) this.binding).recycler.setAdapter(this.adapter);
        this.adapter.setItemListener(this);
        ((ActivityOfficialLayoutBinding) this.binding).statusLayout.setOnRetryListener(new View.OnClickListener() {
            /* class com.grass.appointment.activity.OfficialActivity.View$OnClickListenerC27182 */

            public void onClick(View view) {
                OfficialActivity.this.lastId = 0;
                OfficialActivity.this.getInfo();
            }
        });
        CheckPerMiss();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void init(String str) {
        if (TextUtils.isEmpty(str)) {
            str = "全国";
        }
        ((ActivityOfficialLayoutBinding) this.binding).setCityAddress("當前位置：" + str);
        getInfo();
        AddressDialog addressDialog2 = new AddressDialog(getActivity(), new View.OnClickListener() {
            /* class com.grass.appointment.activity.OfficialActivity.View$OnClickListenerC27193 */

            public void onClick(View view) {
                OfficialActivity.this.addressDialog.dismiss();
                if (!OfficialActivity.this.isOnClick()) {
                    ARouter.getInstance().build(Constance.CITY_SELECT_ACTIVITY).withInt("type", 0).navigation();
                }
            }
        });
        this.addressDialog = addressDialog2;
        addressDialog2.setMessage("定位您當前在" + str + "，是否\n切換？");
        this.addressDialog.show();
    }

    public void CheckPerMiss() {
        if (Build.VERSION.SDK_INT >= 23) {
            new RxPermissions(this).request("android.permission.ACCESS_COARSE_LOCATION").subscribe(new C2753b(this));
        } else {
            getLocation();
        }
    }

    public /* synthetic */ void lambda$CheckPerMiss$0$OfficialActivity(Boolean bool) throws Exception {
        if (bool.booleanValue()) {
            getLocation();
        } else {
            init("");
        }
    }

    private boolean checkGPSIsOpen() {
        return ((LocationManager) getActivity().getSystemService("location")).isProviderEnabled("network");
    }

    private void getLocation() {
        if (!checkGPSIsOpen()) {
            init("");
            return;
        }
        LocationManager locationManager = (LocationManager) getActivity().getSystemService("location");
        this.mLocationManager = locationManager;
        try {
            String city2 = getCity(locationManager.getLastKnownLocation("network"));
            if (!TextUtils.isEmpty(city2)) {
                init(city2);
                return;
            }
            this.mLocationManager.requestLocationUpdates("network", 0, 0.0f, this.locationListener);
            new CountDownTimer(3000, 1000) {
                /* class com.grass.appointment.activity.OfficialActivity.CountDownTimerC27204 */

                public void onTick(long j) {
                }

                public void onFinish() {
                    if (!OfficialActivity.this.isTimeOut) {
                        OfficialActivity.this.mLocationManager.removeUpdates(OfficialActivity.this.locationListener);
                        OfficialActivity.this.init("");
                    }
                }
            }.start();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }

    private String getCity(Location location) {
        List<Address> list = null;
        if (location == null) {
            return null;
        }
        try {
            list = new Geocoder(this, Locale.getDefault()).getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list == null || list.size() <= 0 || list.get(0) == null) {
            return "";
        }
        String locality = list.get(0).getLocality();
        LogUtils.m63e("address", locality);
        return locality;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void updateLocation(Location location) {
        this.isTimeOut = true;
        String city2 = getCity(location);
        this.mLocationManager.removeUpdates(this.locationListener);
        init(city2);
    }

    @Override // androidx.fragment.app.FragmentActivity
    public void onResume() {
        super.onResume();
        this.isResume = true;
        String logo = SpUtils.getInstance().getUserInfo().getLogo();
        if (!TextUtils.isEmpty(logo)) {
            ((ActivityOfficialLayoutBinding) this.binding).setImgCover(logo);
        }
    }

    @Override // androidx.fragment.app.FragmentActivity
    public void onPause() {
        super.onPause();
        this.isResume = false;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void getInfo() {
        if (this.lastId == 0) {
            OfficialAdapter officialAdapter = this.adapter;
            if (!(officialAdapter == null || officialAdapter.getData() == null || this.adapter.getData().size() <= 0)) {
                this.adapter.clear();
            }
            if (!NetUtil.isNetworkAvailable()) {
                ((ActivityOfficialLayoutBinding) this.binding).statusLayout.showNoNet();
                return;
            }
            ((ActivityOfficialLayoutBinding) this.binding).statusLayout.showLoading();
        }
        this.model.getInfo(this.city, this.lastId);
    }

    public static void setUserHead(ImageView imageView, String str) {
        GlideUtils.loadCircleHeadWhite(SpUtils.getInstance().getString("domain") + str, imageView);
    }

    @Override // com.androidx.p013lv.base.listener.ItemClickListener
    public void onItemClick(View view, int i) {
        if (!isOnClick()) {
            ARouter.getInstance().build(Constance.USER_INFO_ACTIVITY).withInt(Key.USER_ID, ((OfficialBean) this.adapter.getDataInPosition(i)).getUserId()).navigation();
        }
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
    public void onLoadMore(RefreshLayout refreshLayout) {
        OfficialAdapter officialAdapter = this.adapter;
        if (officialAdapter == null || officialAdapter.getData() == null || this.adapter.getData().size() == 0) {
            ((ActivityOfficialLayoutBinding) this.binding).refresh.finishLoadMore();
            return;
        }
        this.lastId = ((OfficialBean) this.adapter.getLastDataInPosition()).getUserId();
        getInfo();
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnRefreshListener
    public void onRefresh(RefreshLayout refreshLayout) {
        this.lastId = 0;
        getInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeCityEventClick(ChangeCityEvent changeCityEvent) {
        if (changeCityEvent.getType() == 0) {
            this.city = changeCityEvent.getCityEntity().getName();
            ((ActivityOfficialLayoutBinding) this.binding).setCityAddress("當前位置：" + this.city);
            this.lastId = 0;
            getInfo();
        }
    }

    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, com.androidx.p013lv.base.p015ui.BaseActivity
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        OfficialModel officialModel = this.model;
        if (officialModel != null) {
            officialModel.cancelHttp();
        }
    }

    public class OnClick {
        public OnClick() {
        }

        public void btnBack() {
            OfficialActivity.this.finish();
        }

        public void userInfo() {
            if (!OfficialActivity.this.isOnClick()) {
                ARouter.getInstance().build(Constance.USER_INFO_ACTIVITY).withInt(Key.USER_ID, SpUtils.getInstance().getUserInfo().getUserId()).withInt("type", 0).navigation();
            }
        }

        public void goMessage() {
            ARouter.getInstance().build(Constance.MESSAGE_ACTIVITY).withString(Key.TXT, "嫩模").navigation();
        }

        public void changeCity() {
            if (!OfficialActivity.this.isOnClick()) {
                ARouter.getInstance().build(Constance.CITY_SELECT_ACTIVITY).withInt("type", 0).navigation();
            }
        }
    }
}
